package com.kzd.gtgame.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.entity.*
import com.kzd.gtgame.jetpack.manager.MoneyRepository
import com.kzd.gtgame.utils.httpSign1
import com.kzd.gtgame.utils.httpTime
import com.module.lemlin.http.*

class MoneyViewModel(application: Application) : BaseViewModel(application) {

    private val mMoneyRepository = MoneyRepository()

    private var page: Int = 1
    private var limit: Int = 50

    val moneyDataLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<MoneyData>>>()
    fun moneyData() {
        setRxLifeScope(SubscribeViewModel(moneyDataLiveData)) {
            mMoneyRepository.moneyData()
        }
    }

    val moneySignLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<MoneySign>>>()
    fun moneySign() {
        setRxLifeScope(SubscribeViewModel(moneySignLiveData)) {
            mMoneyRepository.moneySign()
        }
    }

    val moneyTaskNewLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<MoneyTaskNew>>>()
    fun moneyTaskNew() {
        setRxLifeScope(SubscribeViewModel(moneyTaskNewLiveData)) {
            mMoneyRepository.moneyTaskNew()
        }
    }

    val moneyTaskNewGetLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskNewGet(type: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskNewGetLiveData)) {
            mMoneyRepository.moneyTaskNewGet(type)
        }
    }

    val moneyTaskAddShareLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskAddShare() {
        setRxLifeScope(SubscribeViewModel(moneyTaskAddShareLiveData)) {
            mMoneyRepository.moneyTaskAddShare()
        }
    }

    val moneyTaskLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyTask>>>()
    fun moneyTaskDay() {
        setRxLifeScope(SubscribeViewModel(moneyTaskLiveData)) {
            mMoneyRepository.moneyTaskDay()
        }
    }

    val moneyTaskDayGetLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskDayGet(type: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskDayGetLiveData)) {
            mMoneyRepository.moneyTaskDayGet(type)
        }
    }

    val moneySignSubmitLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneySignSubmit() {
        setRxLifeScope(SubscribeViewModel(moneySignSubmitLiveData)) {
            mMoneyRepository.moneySignSubmit()
        }
    }

    val moneyTaskPlayLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyTaskPlay>>>()

    fun moneyTaskPlay(isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<MoneyTaskPlay>>(moneyTaskPlayLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<MoneyTaskPlay>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mMoneyRepository.moneyTaskPlay(page.toString(), limit.toString())
        }
    }

    val moneyTaskPlayGetLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskPlayGet(id: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskPlayGetLiveData)) {
            mMoneyRepository.moneyTaskPlayGet(id)
        }
    }

    val moneyTaskMallLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyTaskMall>>>()

    fun moneyTaskMall() {
        setRxLifeScope(SubscribeViewModel(moneyTaskMallLiveData)) {
            mMoneyRepository.moneyTaskMall()
        }
    }

    val moneyTaskMallDetailsLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataT<MallDetails>>>()

    fun moneyTaskMallDetails(id: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskMallDetailsLiveData)) {
            mMoneyRepository.moneyTaskMallDetails(id)
        }
    }

    val moneyTaskMallQueryLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskMallQuery(id: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskMallQueryLiveData)) {
            mMoneyRepository.moneyTaskMallQuery(id)
        }
    }

    val moneyTaskMallSubmitLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<String>>>()
    fun moneyTaskMallSubmit(goodId: String, addressId: String, num: Int) {
        setRxLifeScope(SubscribeViewModel(moneyTaskMallSubmitLiveData)) {
            mMoneyRepository.moneyTaskMallSubmit(
                httpTime,
                httpSign1(SPSConstance.uid),
                goodId,
                addressId,
                num.toString()
            )
        }
    }

    val moneyBossSharedLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyBossShared>>>()

    fun moneyBossShared() {
        setRxLifeScope(SubscribeViewModel(moneyBossSharedLiveData)) {
            mMoneyRepository.moneyBossShared()
        }
    }

    val moneyBossInviteLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyBossInvite>>>()

    fun moneyBossInvite() {
        setRxLifeScope(SubscribeViewModel(moneyBossInviteLiveData)) {
            mMoneyRepository.moneyBossInvite()
        }
    }


    val moneyExperienceLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyExperience>>>()

    fun moneyExperience(isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<MoneyExperience>>(moneyExperienceLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<MoneyExperience>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mMoneyRepository.moneyExperience(
                page.toString(), limit.toString()
            )
        }
    }


}